import json


def count_keystroke_groups(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            data = json.load(file)
            subject_count = {}
            subject_keystrokes = {}
            total_count = 0

            for entry in data:
                subject = entry['subject']
                keystrokes = entry['keystrokes']
                group_count = len(keystrokes) // 10

                if subject in subject_count:
                    subject_count[subject] += group_count
                else:
                    subject_count[subject] = group_count

                total_count += group_count

                if subject not in subject_keystrokes:
                    subject_keystrokes[subject] = []
                subject_keystrokes[subject].extend(keystrokes)

            for subject, count in subject_count.items():
                # 检查击键信息是否完整
                is_complete = True
                missing_info = []
                keystrokes = subject_keystrokes[subject]
                for i, keystroke in enumerate(keystrokes):
                    if 'key_content' not in keystroke or not isinstance(keystroke['key_content'], str) or len(keystroke['key_content']) != 1:
                        is_complete = False
                        missing_info.append(f"第 {i + 1} 个 keystroke 的 key_content 不是单个按键")
                    if 'press_time' not in keystroke or not isinstance(keystroke['press_time'], (int, float)):
                        is_complete = False
                        missing_info.append(f"第 {i + 1} 个 keystroke 的 press_time 不是数字")
                    if 'release_time' not in keystroke or not isinstance(keystroke['release_time'], (int, float)):
                        is_complete = False
                        missing_info.append(f"第 {i + 1} 个 keystroke 的 release_time 不是数字")

                if is_complete:
                    print(f"用户 {subject}：{count} 组击键数据——击键信息完整")
                else:
                    print(f"用户 {subject}：{count} 组击键数据——击键信息不完整：")
                    for info in missing_info:
                        print(info)

            print(f"所有用户的数组总个数：{total_count}")

    except FileNotFoundError:
        print("错误: 文件未找到!")
    except json.JSONDecodeError:
        print("错误: 无法解析 JSON 文件!")
    except Exception as e:
        print(f"错误: 发生了一个未知错误: {e}")


if __name__ == "__main__":
    file_path = 'assets\\database\\main_database.json'
    count_keystroke_groups(file_path)
    